/*
 * Copyright 2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.integtests.resolve.attributes

import org.gradle.integtests.fixtures.AbstractIntegrationSpec

class ArtifactViewArtifactSelectionIntegrationTest extends AbstractIntegrationSpec {
    def setup() {
        settingsFile << """
            rootProject.name = "consumer"
            include "producer"
        """
        buildFile << """
            configurations {
                resolveConf {
                    canBeConsumed = false
                    canBeResolved = true
                } 
            }
        """
        file("producer/output").text = "from project producer"
        file("producer/alternative").text = "alternative from project producer"
    }

    def "keeps chosen configuration through artifact view"() {
        file("producer/build.gradle") << """
            configurations {
                conf {
                    canBeConsumed = true
                    canBeResolved = false
                }
                confWithAttributes {
                    canBeConsumed = true
                    canBeResolved = false
                    attributes {
                        attribute(Attribute.of("usage", String), "alternative")
                    }
                }
            }
            artifacts {
                conf file("output")
                confWithAttributes file("alternative")
            }
        """
        buildFile << """
            dependencies {
                resolveConf project(path: ":producer", configuration: "conf")
            }
            task resolve(type: Sync) {
                from(configurations.resolveConf.incoming.artifactView {
                    attributes {
                        attribute(Attribute.of("usage", String), "alternative")
                    }
                }.files)
                into(buildDir)
            }
        """
        when:
        succeeds("resolve")
        then:
        file("build/alternative").assertDoesNotExist()
        file("build/output").assertExists()
    }

    def "can depend on two configurations from the same project"() {
        file("producer/build.gradle") << """
            configurations {
                conf {
                    canBeConsumed = true
                    canBeResolved = false
                }
                additionalConf {
                    canBeConsumed = true
                    canBeResolved = false
                }
            }
            artifacts {
                conf file("output")
                additionalConf file("alternative")
            }
        """
        buildFile << """
            dependencies {
                resolveConf project(path: ":producer", configuration: "conf")
                resolveConf project(path: ":producer", configuration: "additionalConf")
            }
            task resolve(type: Sync) {
                from(configurations.resolveConf)
                into(buildDir)
            }
        """
        when:
        succeeds("resolve")
        then:
        file("build/output").assertExists()
        file("build/alternative").assertExists()
    }
}
